package com.yonyou.pmclouds.basecom.dao.impl.mybatis.interceptor;
 
import java.util.Properties;

import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Plugin;
import org.apache.ibatis.plugin.Signature;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds; 
 
/**
 * 将分页参数加入cache条件。不使用mybatis的cache时可不使用此拦截器。 
 * 
 * @author zm 
 */ 
@Intercepts({ @Signature(type = Executor.class, method = "query", args = { MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class}) }) 
public class RowBoundsInterceptor implements Interceptor { 
  
 private static ThreadLocal<RowBounds> rowBounds = new ThreadLocal<RowBounds>(); 
  
 public static RowBounds getRowBounds() { 
  RowBounds rowBounds = RowBoundsInterceptor.rowBounds.get(); 
  RowBoundsInterceptor.rowBounds.remove(); 
  return rowBounds; 
 } 
 
 public static void setRowBounds(RowBounds rowBounds) { 
  RowBoundsInterceptor.rowBounds.set(rowBounds); 
 } 
  
 @Override 
 public Object intercept(Invocation invocation) throws Throwable { 
        Object[] args = invocation.getArgs(); 
 
  RowBounds rowBounds = getRowBounds(); 
  if (rowBounds != null) { 
            args[2] = rowBounds; 
        } 
 
        return invocation.proceed(); 
 } 
 
 @Override 
 public Object plugin(Object target) { 
  return Plugin.wrap(target, this); 
 } 
 
 @Override 
 public void setProperties(Properties properties) {} 
 
}
